var app = getApp()
var api = require('../../config/api.js');

Page({

  /**
   * 页面的初始数据
   */

  data: {
    value1: 1,
    transExpense: 8,
    money: 0,
    lists: [],

    ordlists: [],

    addreslist: {
      trueName: '',
      mobile: '',
      address: '',
      note: ''
    },
    orderDetailsList: [],

    orderNo: '',
    orderDetailsList: [],
    totalNum: 0,
    status: '',

    expressName:'',
    expressNumber:'',
  },

  maginfo: function (e) {
    console.log('e.currentTarget.id' + e.currentTarget.id)
    wx.navigateTo({
      url: '/pages/techgoodlist/techgoodinfo/techgoodinfo?id=' + e.currentTarget.id,
    })
  },

  //获取快递名称
  expressNameinput: function (e) {
    this.setData({
      expressName: e.detail.value
    })
  },

  //获取快递单号
  expressNumberinput: function (e) {
    this.setData({
      expressNumber: e.detail.value
    })
  },

  sendg: function (e) {
    var that = this;
    wx.showModal({
      title: '交易提示',
      content: '确认是否发货？',
      confirmColor: "#43CBC9",
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })

        if (res.confirm) {
          console.log('for发生了submit事件，携带数据为：', e.detail.value);

          if (that.data.expressName == "") {
            wx.showToast({
              title: "请填写发货快递名!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (that.data.expressNumber == "") {
            wx.showToast({
              title: "请填写发货快单号!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } 

          var orderNo = that.data.orderNo;
          console.log("orderNo=" + orderNo)
          wx.request({
            url: api.OrdersDeli ,
            data: {
              orderNo: orderNo,
              expressName: that.data.expressName,
              expressNumber: that.data.expressNumber,
              status: '1',
            },
            method: 'POST',
            header: {
              'Accept': 'application/json',
              'ACCESS-TOKEN': wx.getStorageSync('token')
            },

            success: function (res) {
              if (res.data.success) {
                wx.showModal({
                  title: '交易提示',
                  content: "发货成功!",
                  confirmColor: "#43CBC9",
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.navigateBack({})
                    }
                  }
                });
                wx.hideLoading();
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 3000
                });
              }
            },
            fail: function () {
              wx.hideLoading();
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 3000
              });
            },
          })
        } else if (res.cancel) {

          wx.showToast({
            title: "交易已取消",
            icon: 'none',
            duration: 2000
          });
          console.log('用户点击取消')
          wx.hideLoading();
        }
      }

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    that.setData({
      orderNo: options.id,
    })

    wx.request({
      url: api.OrdersInfoGet+ that.data.orderNo,
      data: {},
      method: 'GET',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })
        if (res.data.success) {
          var st = res.data.obj.status;
          console.log('st = ' + st)
          that.setData({
            status: st,
          })


          if (st == '0') {
            that.setData({

            })
            console.log(0)
          } else if (st == '1') {
            that.setData({
              payDate: res.data.obj.payDate,
            })
            console.log(1)
          } else if (st == '2') {
            that.setData({
              payDate: res.data.obj.payDate,
            })
            console.log(2)
          } else if (st == '3') {
            that.setData({
              payDate: res.data.obj.payDate,
            })
          } else if (st == '4') {
            that.setData({
              payDate: res.data.obj.payDate,
            })
          }


          that.setData({
            orderDetailsList: res.data.obj.orderDetailsList,
            goodsSales: res.data.obj.goodsSales,
            receiverAddress: res.data.obj.receiverAddress,
            receiverName: res.data.obj.receiverName,
            receiverPhone: res.data.obj.receiverPhone,
            comment: res.data.obj.comment,
            createDate: res.data.obj.createDate,
            transportationExpense: res.data.obj.transportationExpense,
            discounts: res.data.obj.discounts.toFixed(2),
            actualPayment: res.data.obj.actualPayment,
            expressNames: res.data.obj.expressName,
            expressNumbers: res.data.obj.expressNumber,
          });

          var orderlist = that.data.orderDetailsList;
          var num = 0;
          for (var i = 0; i < that.data.orderDetailsList.length; i++) {
            num = num + orderlist[i].goodsNum;
          }
          console.log('num=' + num);
          that.setData({
            totalNum: num,
          })

          wx.hideLoading();
        }
      }
    })

    console.log('orderNo=' + that.data.orderNo)



  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    var that = this;
    that.setData({
      lists: wx.getStorageSync('lists'),
      addreslist: wx.getStorageSync('addreslist')
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})